<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="主体名称" prop="marketInfoId">
                      <el-input v-model="formData.marketInfoId" placeholder="请输入主体名称" />
                    </el-form-item>
                    <el-form-item label="采样时间" prop="sampleTakeDate">
                      <el-date-picker clearable v-model="formData.sampleTakeDate" type="date" value-format="timestamp" placeholder="选择采样时间" />
                    </el-form-item>
                    <el-form-item label="检测时间" prop="sampleCheckDate">
                      <el-date-picker clearable v-model="formData.sampleCheckDate" type="date" value-format="timestamp" placeholder="选择检测时间" />
                    </el-form-item>
                    <el-form-item label="检查试剂" prop="checkReagent">
                      <el-input v-model="formData.checkReagent" placeholder="请输入检查试剂" />
                    </el-form-item>
                    <el-form-item label="数据来源" prop="dataSourceId">
                      <el-select v-model="formData.dataSourceId" placeholder="请选择数据来源id">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DATA_SOURCE_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="检测结果" prop="checkResult">
                      <el-select v-model="formData.checkResult" placeholder="请选择检测结果">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CHECK_RESULT)"
                                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="样本类型id" prop="simpleTypeId">
                      <el-select v-model="formData.simpleTypeId" placeholder="请选择样本类型id">
                            <el-option label="请选择字典生成" value="" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="样本类型" prop="simpleTypeName">
                      <el-input v-model="formData.simpleTypeName" placeholder="请输入样本类型" />
                    </el-form-item>
                    <el-form-item label="样本子类id" prop="simpleTowLevelId">
                      <el-select v-model="formData.simpleTowLevelId" placeholder="请选择样本子类id">
                            <el-option label="请选择字典生成" value="" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="样本子类" prop="simpleTowLevelName">
                      <el-input v-model="formData.simpleTowLevelName" placeholder="请输入样本子类" />
                    </el-form-item>
                    <el-form-item label="样品id" prop="simpleId">
                      <el-select v-model="formData.simpleId" placeholder="请选择样品id">
                            <el-option label="请选择字典生成" value="" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="样品名称" prop="simpleName">
                      <el-input v-model="formData.simpleName" placeholder="请输入样品名称" />
                    </el-form-item>
                    <el-form-item label="检查项目" prop="checkType">
                      <el-select v-model="formData.checkType" placeholder="请选择检查项目">
                            <el-option label="请选择字典生成" value="" />
                      </el-select>
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as CheckDataApi from '@/api/market/CheckData';
      export default {
    name: "CheckDataForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            marketInfoId: undefined,
                            sampleTakeDate: undefined,
                            sampleCheckDate: undefined,
                            checkReagent: undefined,
                            dataSourceId: undefined,
                            checkResult: undefined,
                            simpleTypeId: undefined,
                            simpleTypeName: undefined,
                            simpleTowLevelId: undefined,
                            simpleTowLevelName: undefined,
                            simpleId: undefined,
                            simpleName: undefined,
                            checkType: undefined,
        },
        // 表单校验
        formRules: {
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await CheckDataApi.getCheckData(id);
            this.formData = res.data;
            this.title = "修改检测日志";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增检测日志";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await CheckDataApi.updateCheckData(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await CheckDataApi.createCheckData(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            marketInfoId: undefined,
                            sampleTakeDate: undefined,
                            sampleCheckDate: undefined,
                            checkReagent: undefined,
                            dataSourceId: undefined,
                            checkResult: undefined,
                            simpleTypeId: undefined,
                            simpleTypeName: undefined,
                            simpleTowLevelId: undefined,
                            simpleTowLevelName: undefined,
                            simpleId: undefined,
                            simpleName: undefined,
                            checkType: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
